#include "util.h"
#include <TTree.h>
#include "TNtuple.h"
#include "math.h"
#include <fstream>
#include <iostream>
#include "TFileCollection.h"
#include "TCollection.h"

using namespace std;

Double_t getPt(Double_t px, Double_t py)
{
  return sqrt(pow(px,2)+pow(py,2));
}
Double_t calcWeight(TString fileName, Double_t numEvents)
{
  Double_t crossSection=0.001;
  Double_t weight(1.0), nev(1.0);
  /* crossSection numbers are in pico barn */
  (fileName=="su4_106400")?(crossSection=47.925,nev=numEvents)
    :(crossSection=0.001, nev=1.e3);
  
  weight=(crossSection*1000.)/nev; 
  return weight;
}

void addFiles( TString fileName, TChain *chain, TString chFile )
{
  cout << " Adding Files for process: " << fileName << endl;
  
  //   if( strcmp(chFile,"mSUGRA") > 0){
  //cout << chFile << endl;
  TFileCollection fc("","",chFile);
  TCollection *mylist = (TCollection*)fc.GetList();
  chain->AddFileInfoList(mylist);
  // }
  //   else  chain->Add(chFile);
}
Double_t invMass2(Double_t px1, Double_t py1, Double_t pz1, Double_t E1,
               Double_t px2, Double_t py2, Double_t pz2, Double_t E2)
{
  Double_t invmass2=pow((E1+E2),2)
    - pow((px1+px2),2)
    - pow((py1+py2),2)
    - pow((pz1+pz2),2);
    
  if(invmass2 >0)return (sqrt(invmass2));
  else return 0;
}

Double_t deltaR(TLorentzVector t1, TLorentzVector t2)
{
  double Pi=TMath::Pi();
  Double_t deltaEta = fabs(t1.Eta() - t2.Eta());
  Double_t deltaPhi = fabs(t1.Phi() - t2.Phi());
  if(deltaPhi>Pi)deltaPhi=2*(Pi)-deltaPhi;
  Double_t deltaR = sqrt(pow(deltaEta,2) + pow(deltaPhi,2));
  return deltaR;
}
Double_t calcMLL(TLorentzVector t1, TLorentzVector t2)
{
  TLorentzVector tmp_tlv;
  tmp_tlv=t1+t2;
  Double_t mass = tmp_tlv.M();
  return(mass);
}
Double_t caldphi(Double_t t1, Double_t t2)
{
  double Pi=TMath::Pi();
  Double_t deltaPhi = TMath::Abs(t1 - t2);
  if(deltaPhi>Pi)deltaPhi=(2*Pi)-deltaPhi;
  return deltaPhi;
}
Double_t caldeta(Double_t t1, Double_t t2)
{
  Double_t deltaEta = TMath::Abs(t1 - t2);
  return deltaEta;
}
Double_t caldr(Double_t t1, Double_t t2)
{
  Double_t deltar=sqrt( pow(t1,2) + pow(t2,2) );
  return( deltar );
}
Double_t calmindr(Double_t t1, Double_t t2, std::vector<TLorentzVector> tv)
{
  Double_t mindr=10000.;
  for(UInt_t i=0; i<tv.size(); i++){
    double _deta=caldeta(t1, tv.at(i).Eta());
    double _dphi=caldphi(t2, tv.at(i).Phi());
    double _dr = caldr(_deta, _dphi);
    if(_dr<mindr)mindr=_dr;
  }
  return mindr;
}
